package com.ruoyi.huanghefd.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.huanghefd.domain.Worksheet;
import com.ruoyi.huanghefd.service.WorksheetService;
import com.ruoyi.huanghefd.utils.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author gnplum
 * @Date 2023/10/30 16:15
 * @PackageName:com.ruoyi.huanghefd.controller
 * @ClassName: WorkSheetController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
@RequestMapping("/huanghefd")
public class WorksheetController {
    private static final Logger log = LoggerFactory.getLogger(WorksheetController.class);


    @GetMapping("/test")
    public String test(HttpServletResponse response) throws IOException {
        return "接口测试成功！！！！！";
    }

    @Resource
    private WorksheetService worksheetService;

      /**
     * @Author gnplum
     * @Description //新增工作票
     * @Date 9:48 2023/10/31
     * @param worksheets
     * @return com.ruoyi.huanghefd.domain.ResultWS
     * http://localhost:8080/huanghefd/addWorksheet
     **/
    @PostMapping("/addWorksheet")
    public AjaxResult addWorksheet(@RequestBody Worksheet worksheets){
        log.info(">> 新增工作票-入参==" + JSON.toJSONString(worksheets));
        AjaxResult ajax = worksheetService.addWorksheet(worksheets);
        log.info("<< 新增工作票-返回==" + ajax.toString());
        return ajax;
    }

    /**
     * @Author gnplum
     * @Description //删除工作票
     * @Date 15:22 2023/10/31
     * @param wsCode
     * @return com.ruoyi.common.core.domain.AjaxResult
     * http://localhost:8080/huanghefd/delWorksheet?wsId=4e60df22773811eeb08e00e04c738ae4
     **/
    @PostMapping(value = "/delWorksheet")
    public AjaxResult delWorksheet(@RequestParam(required = false,value = "wsCode") String wsCode){
        log.info(">> 删除工作票-入参 wsCode==" + wsCode);
        if(wsCode.isEmpty()){
            return AjaxResult.success("请点选要删除的工作票");
        }
        AjaxResult ajax = worksheetService.delWorksheet(wsCode);
        log.info(">> 删除工作票-返回==" + ajax);
        return ajax;
    }

    /**
     * @Author gnplum
     * @Description //修改工作票
     * @Date 15:47 2023/10/31
     * @param worksheets
     * @return com.ruoyi.common.core.domain.AjaxResult
     * http://localhost:8080/huanghefd/uptWorksheet
     **/
    @PostMapping(value = "/uptWorksheet")
    public AjaxResult uptWorksheet(@RequestBody Worksheet worksheets){
        log.info(">> 修改工作票-入参 ==" + JSON.toJSONString(worksheets));
        AjaxResult ajax = worksheetService.uptWorksheet(worksheets);
        log.info(">> 修改工作票-返回 ==" + ajax);
        return ajax;
    }

    /**
     * @Author gnplum
     * @Description //查询工作票
     * @Date 9:48 2023/10/31
     * @param wsCode,wsDept,wsState
     * @return com.ruoyi.common.core.domain.AjaxResult
     * http://localhost:18080/huanghefd/queryWorksheet?wsCode=20231102000001&wsDept=100
     **/
    @PostMapping(value = "/queryWorksheet")
    public PageBean queryWorksheetList(@RequestParam(required = false, value = "wsCode") String wsCode, @RequestParam(required = false, value = "wsDept") String wsDept, @RequestParam(required = false, value = "wsState") String wsState, @RequestParam(required = false, value = "userId") String userId, @RequestParam(required = false, value = "mailType") String mailType, @RequestParam(required = false, value = "wsType") String wsType, @RequestParam(value = "pageNum") int pageNum, @RequestParam(value = "pageSize") int pageSize) {
        log.info(">> 查询工作票-入参==" + wsCode + "," + wsDept + "," + wsState + "," + userId + "," + mailType + "," + wsType);
        PageBean ajax = worksheetService.queryWorksheetByPage(wsCode, wsDept, wsState, userId, mailType, wsType, pageNum, pageSize);//mailType 1-收件箱 2-发件箱 3-草稿箱
        log.info(">> 查询工作票-返回==" + ajax);
        return ajax;
    }
    @PostMapping(value = "/queryWorksheet1")
    public AjaxResult queryWorksheetList(@RequestParam(required = false,value = "wsCode") String wsCode, @RequestParam(required = false,value = "wsDept") String wsDept, @RequestParam(required = false,value = "wsState") String wsState, @RequestParam(required = false,value = "userId") String userId, @RequestParam(required = false,value = "wsType") String wsType, @RequestParam(required = false,value = "mailType") String mailType){
        log.info(">> 查询工作票-入参==" + wsCode + "," + wsDept + "," + wsState + "," + userId + "," + mailType + "," + wsType);
        AjaxResult ajax= worksheetService.queryWorksheets(wsCode,wsDept,wsState,userId,mailType);//mailType 1-收件箱 2-发件箱 3-草稿箱
        log.info(">> 查询工作票-返回==" + ajax);
        return ajax;
    }

    /**
     * @Author gnplum
     * @Description //存在正在执行的工作票，则不允许创建 检验
     * @Date 21:23 2023/12/20
     * @param userId
    * @return com.ruoyi.common.core.domain.AjaxResult
    **/
    @GetMapping(value = "/verifyCreateSheets")
    public AjaxResult verifySign(@RequestParam(required = true, value = "userId") String userId) {
        log.info("工作负责人是否存在正在执行的票？>>" + userId);
        return worksheetService.verifyCreateSheets(userId);
    }

}